package com.rzgeo.rzmaplib.model;

import android.app.Activity;

import com.esri.arcgisruntime.data.ShapefileFeatureTable;
import com.esri.arcgisruntime.data.TileCache;
import com.esri.arcgisruntime.layers.ArcGISTiledLayer;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.layers.Layer;
import com.rzgeo.rzmaplib.comp.geoview.IRmlGeoViewHolder;
import com.rzgeo.rzmaplib.util.IRmlLoadListener;

import java.io.File;
import java.util.ArrayList;

/**
 * Created by zhanghang on 2018/1/21.
 */

public class RmlPrjMapLoader {
    private RmlPrj _prj;
    private IRmlGeoViewHolder _geoViewHolder
            ;
//    private final OptionalLayerMgr _optionalLayerMgr;

    public RmlPrjMapLoader(Activity activity, RmlPrj prj, IRmlGeoViewHolder geoViewHolder, final IRmlLoadListener loadListener) {
        _prj=prj;
        _geoViewHolder=geoViewHolder;

        loadBaseMap(prj,loadListener);

        loadOptionalMap(prj);

        loadEditorMap(prj);

//        _optionalLayerMgr=new OptionalLayerMgr(activity,geoViewHolder);
    }

    private void loadBaseMap(RmlPrj prj, final IRmlLoadListener loadListener) {
        if(prj.getBaseMap().doms.length>0){
            TileCache tc = new TileCache(prj.getBaseMap().doms[0].getAbsolutePath());

            final ArcGISTiledLayer tlayer = new ArcGISTiledLayer(tc);

            tlayer.addDoneLoadingListener(new Runnable() {
                @Override
                public void run() {
                    _geoViewHolder.setViewExtent(tlayer.getFullExtent());
                    loadListener.Loaded(true,null);
                    tlayer.removeDoneLoadingListener(this);
                }
            });

            if(prj.getBaseMap().dems.length==0){
                _geoViewHolder.setBaseMap(tlayer,null);

            }else{
                _geoViewHolder.setBaseMap(
                        tlayer,
                        prj.getBaseMap().dems[0].getAbsolutePath()
                );
            }

            if(prj.getBaseMap().doms.length>1){
                ArrayList<Layer>other2Add=new ArrayList<>(prj.getBaseMap().dems.length-1);

                for(int i=1;i<prj.getBaseMap().doms.length;i++)
                {
                    File domFile=prj.getBaseMap().doms[i];
                    TileCache tc2 = new TileCache(prj.getBaseMap().doms[0].getAbsolutePath());
                    other2Add.add(new ArcGISTiledLayer(tc2));
                }
                _geoViewHolder.addDomLayer(other2Add);
            }
        }
    }

    private void loadOptionalMap(RmlPrj prj) {
//        for (File lFile:prj.getOptionalMap().shps) {
//            ShapefileFeatureTable featureTable=new ShapefileFeatureTable(lFile.getAbsolutePath());
//            Layer fl=new FeatureLayer(featureTable);
//            _geoViewHolder.addOptionLayer(fl);
//        }

        _geoViewHolder.adddRefenceLayers(prj.getOptionalMap().getLayers());

    }
    private void loadEditorMap(RmlPrj prj) {
//        for (File lFile:prj.getEditMap().getAllEditableLayerFiles()) {
//            ShapefileFeatureTable featureTable=new ShapefileFeatureTable(lFile.getAbsolutePath());
//            FeatureLayer fl=new FeatureLayer(featureTable);
//            prj.getEditMap().setLayerOfFile(lFile,fl);
//            _geoViewHolder.addOptionLayer(fl);
//        }
        _geoViewHolder.adddRefenceLayers(prj.getEditMap().getLayers());
    }

    public void getFullExtent() {

    }

    public RmlPrj getPrj() {
        return _prj;
    }

    public void release() {
        _geoViewHolder=null;
        _prj.release();
        _prj=null;
    }

//    public static class OptionalLayerMgr {
//        private DefaultMapViewOnTouchListener _touchListener;
//
//        private final IRmlGeoViewHolder _geoViewHolder;
//        private OnIdentityListener _listener;
//
//        public OptionalLayerMgr(Activity activity, IRmlGeoViewHolder geoViewHolder) {
//            _geoViewHolder=geoViewHolder;
//            _touchListener=new DefaultMapViewOnTouchListener(activity,)
//        }
//
//        public void startIdentity() {
//            _geoViewHolder.getGeoView().setOnTouchListener(_touchListener);
//        }
//
//        public interface OnIdentityListener{
//            void onReuslt(Map<Layer,ArrayList<Feature>>results);
//        }
//
//        public void setIdentityListener(OnIdentityListener listener) {
//            _listener=listener;
//        }
//    }
}
